// index.js
const { spawn } = require('child_process');
var model = function() {
  this.messageList = []
}
model.prototype.getMessage = function() {
	return this.messageList
}
model.prototype.callChatGPT = function(content,cb) {
  var self = this
  self.messageList.push({"role": "user","content":content})
  const pythonProcess = spawn('python', ['./compose/chatGPT/hello.py',JSON.stringify(self.messageList)]);

  pythonProcess.stdout.on('data', (data) => {
    console.log(`stdout: ${data}`);
    self.messageList.push({"role": "system","content":`${data}`})
    if(self.messageList.length > 20){
    	self.messageList = self.messageList.slice(-20)
    }
    if(cb)
      cb(self.messageList)
  });

  pythonProcess.stderr.on('data', (data) => {
    console.error(`stderr: ${data}`);
  });

  pythonProcess.on('close', (code) => {
    console.log(`child process exited with code ${code}`);
  });
}
module.exports = new model()
